﻿//#include <vld.h>
#include <QApplication>
#include <QScreen>
#include <QDir>

#include "ElaApplication.h"
#include "mainwindow.h"
#include "BaseModule/GlobalHelper.h"
#include "BaseModule/AppManager.h"


int main(int argc, char* argv[])
{
    // 屏幕适配
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    //根据实际屏幕缩放比例更改
    //qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication a(argc, argv);

    //新UI
    eApp->init();
    pst::MainWindow w;


    // 获取屏幕尺寸和缩放因子  
    int currentScreen = a.desktop()->screenNumber(&w);//程序所在的屏幕编号
    QRect screenGeometry = a.desktop()->screenGeometry(currentScreen);//程序所在屏幕尺寸

    QScreen* screen = QGuiApplication::screens()[currentScreen];
    qreal devicePixelRatio = screen->devicePixelRatio();

    // 占屏幕的 70%（宽和高）  
    double widthRatio = 0.7;
    double heightRatio = 0.7;

    // 计算窗口的宽度和高度  
    int windowWidth = static_cast<int>(screenGeometry.width() * widthRatio / devicePixelRatio);
    int windowHeight = static_cast<int>(screenGeometry.height() * heightRatio / devicePixelRatio);

    // 设置窗口的大小  
    w.resize(windowWidth, windowHeight);

    // 计算居中位置  
    int x = (screenGeometry.width() - windowWidth * devicePixelRatio) / 2;
    int y = (screenGeometry.height() - windowHeight * devicePixelRatio) / 2;

    // 移动窗口到居中位置  
    w.move(x, y);

    AppManagerInstance->init(&w);    
    auto defaultDir = QApplication::applicationDirPath() + "/../examples";
    w.setInputDir(defaultDir);
    if (argc > 2)
    {
        auto inputDir = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[1]));
        auto outDir = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[2]));
        w.setInputDir(inputDir);
        w.setOutputDir(outDir);

        QDir _dir(outDir);
        if (!_dir.exists(outDir))
        {
            _dir.mkpath(outDir);
        }

        std::cout << "inputDir = " << inputDir.toStdString() << std::endl;
        std::cout << "outputDir = " << outDir.toStdString() << std::endl;
    }
    if (argc > 3)
    {
        auto simuType = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[3]));
        w.setSimuType(simuType);
        std::cout << "simuType = " << simuType.toStdString() << std::endl;
    }
    w.show();

    return a.exec();
}
